package net.sxnic.ugr.group;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import net.sxinfo.core.entity.AbstractEntity;
import net.sxnic.ugr.role.Role;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.IndexColumn;


@Entity
@Table(name = "ugr_group")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Group extends AbstractEntity {

	/**
	 * serial
	 */
	private static final long serialVersionUID = 8655423795683869558L;


	/**
	 * 组代码
	 */
	@IndexColumn(name = "groupname_index")
	@Column(unique = true, nullable = false,name="group_code")
	private String group;

	/**
	 * 组名字
	 */
	@Column(nullable = false,name="group_name")
	private String name;
	
	/**
	 * 所属子系统code ,如果未空表示公用，如果有值则表示子系统专用
	 */
	private String siteCode;

	/**
	 * Roles
	 */
	@ManyToMany
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private Set<Role> roles;

	/**
	 * 默认构造器
	 */
	public Group() {
		super();
		roles = new HashSet<Role>();
	}

	/**
	 * 构造器
	 * 
	 * @param group 组名
	 * @param name 组描述
	 */
	public Group(String group, String name) {
		this();
		this.group = group;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}
	
	public void addRole(Role role){
		this.getRoles().add(role);
	}
	
	public void removeRole(Role role){
		if(this.getRoles().contains(role)){
			this.getRoles().remove(role);
		}
	}

	public String getSiteCode() {
		return siteCode;
	}

	public void setSiteCode(String siteCode) {
		this.siteCode = siteCode;
	}
}
